import React, {Component} from 'react';
import Child from "./Child";

class Parent extends Component {

    state = {
        hasError: "" //用于标识子组件是否产生错误
    }

    //当Paren的子组件出现错误时候，会触发getDerivedStateFromError调用，并携带错误信息
    static getDerivedStateFromError(error) {
        return {hasError: error}
    }

    componentDidCatch(error, info) {
        console.log('渲染组件时出错')
    }


    render() {
        return (
            <div>
                <h4>我是Parent组件</h4>
                {this.state.hasError ? <h2>当前网络不稳定，稍后再试</h2> : <Child/>}
            </div>
        );
    }
}

export default Parent;